package ualrcdg.client;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.util.ArrayList;

import org.w3c.dom.Document;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.widget.Toast;


// via Stack Overflow: http://stackoverflow.com/questions/3619372/android-service-for-tcp-sockets
public class CommunicatorService extends Service 
{
	ArrayList<Document> serviceDocumentList;
	Document serviceDoc;
	Socket s;
	InputStream is;
    OutputStream os;
    Document doc;
	
	@Override
	public IBinder onBind(Intent arg0) 
	{
		return myBinder;
	}
	
	private final IBinder myBinder = new LocalBinder();

    public class LocalBinder extends Binder
    {
        public CommunicatorService getService()
        {
            return CommunicatorService.this;
        }
    }

    @Override
    public void onCreate() 
    {
        super.onCreate();
        s = new Socket();
        try 
        {
			is = new FileInputStream((File) doc);
		} 
        catch (FileNotFoundException e) 
        {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        os = new BufferedOutputStream(null);
    }

    public void IsBoundable(){
        Toast.makeText(this,"I bind like butter", Toast.LENGTH_LONG).show();
    }

    public void onStart(Intent intent, int startId){
        super.onStart(intent, startId);
        Toast.makeText(this,"Service created ...", Toast.LENGTH_LONG).show();
    }
    
    public ArrayList<Document> makeConnection(Document doc)
    {
    	ConnectSocket connectSocket = new ConnectSocket();
    	return connectSocket.sendRequest(doc);
    }

	class ConnectSocket implements Runnable 
	{
		private Document request;
		private ArrayList<Document> response;
		
		public ArrayList<Document> sendRequest(Document request)
		{
			this.request = request;
			this.run();
			return this.response;
		}

        @Override
        public void run() 
        {
            SocketAddress socketAddress = new InetSocketAddress("localhost", 4505);
            try 
            {               
                s.connect(socketAddress);
                os = s.getOutputStream();
                is = s.getInputStream();
            } 
            catch (IOException e) 
            {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        try 
        {
        	os.close();
            s.close();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        s = null;
    }

}